﻿Public Class FormVendor

    Private Sub FormVendor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        UI.FormatDataGrid(dgv)

        LoadData()
       
    End Sub
    Private Sub LoadData() 
        Cmd.Parameters("@S") = txtSearch.Text
        Dim dt As DataTable = Cmd.ExecuteDataTable("SELECT * FROM Vendor WHERE Active=1 AND UPPER(VendorName) LIKE UPPER('%'+@S+'%');")
        Me.dgv.DataSource = dt

    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        Dim row = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("VendorId").Value


        Cmd.Parameters("@VendorId") = id
        Cmd.ExecuteNonQuery("UPDATE Vendor SET Active=0 WHERE VendorId=@VendorId")

        LoadData()

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim frm As New FormVendorEditor()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.dgv.SelectedRows(0)
        Dim frm As New FormVendorEditor()
        frm.txtVendorId.Text = row.Cells("VendorId").Value
        frm.txtVendorName.Text = row.Cells("VendorName").Value
        frm.txtPhone.Text = row.Cells("Phone").Value
        frm.txtAddress.Text = row.Cells("Address").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub
End Class